package com.boarsoft.message.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boarsoft.common.util.RandomUtil;
import com.boarsoft.message.bean.Message;
import com.boarsoft.message.core.MessageService;
import com.boarsoft.message.core.Messenger;
import com.boarsoft.message.store.MessageRoller;

public class Main {
	private static Logger log = LoggerFactory.getLogger(Main.class);
	private static ClassPathXmlApplicationContext ctx;

	public static void main(String[] args) {
		ctx = new ClassPathXmlApplicationContext("classpath:conf/context.xml");
		System.out.println("Startup ".concat(ctx.isRunning() ? "successfully." : "failed."));
		
		// for (int i = 0; i < 100; i++) {
		// System.out.print(RandomUtil.random(0, 2));
		// }
		// System.out.println();

		// testDubboMessageAdapter(ctx);

		// MessageContext mc = (MessageContext) ctx.getBean("messageContext");
		// TargetMQ tq = mc.getTargetQueue("sms_debit");
		// testRoller(tq.getRoller());
		// tq = mc.getTargetQueue("queue1");
		// testRoller(tq.getRoller());

//		Runtime.getRuntime().addShutdownHook(new Thread() {
//			public void run() {
//				try {
//					ctx.close();
//				} catch (Exception e) {
//					log.error("Shutdown hook can not close application context", e);
//				}
//			}
//		});

//		Message m = new Message();
//		m.setTarget("queue1");
//		m.addRecipient("queue1Consumer1");
//		m.setContent("hello");
//		try {
//			Messenger.send(m);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

		// testQueue(ctx);
	}

	public static void testQueue(final ClassPathXmlApplicationContext ctx) {
		int tc = 300;
		ExecutorService es = Executors.newFixedThreadPool(tc);
		final CountDownLatch cdl = new CountDownLatch(tc);
		long l = System.currentTimeMillis();
		for (int n = 0; n < tc; n++) {
			es.execute(new Runnable() {
				public void run() {
					for (int i = 0; i < 10; i++) {
						testQueue(ctx, "queue1");
					}
					cdl.countDown();
				}
			});
		}
		try {
			cdl.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis() - l);
	}

	public static void testRoller(MessageRoller roller) {
		StringBuilder sb = new StringBuilder();
		int len = 1;
		for (int i = 0; i < len; i++) {
			sb.append("0123456789");
		}
		String content = sb.toString();
		try {
			// long l = System.currentTimeMillis();
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					for (int k = 0; k < 10; k++) {
						Message m = new Message();
						m.setTarget("queue1");
						m.addRecipient("queue1Consumer1");
						m.setContent(content);
						sb.setLength(0);
						sb.append("DEBUG: flow").append(i).append("/").append(j);
						sb.append("/step").append(k).toString();
						m.setKey(sb.toString());
						m.setId(RandomUtil.genUUID());
						roller.append(m);
					}
				}
			}
			// log.info("Append 10000 messages in {}ms",
			// System.currentTimeMillis() - l);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void testQueue(ClassPathXmlApplicationContext ctx, String target) {
		StringBuilder sb = new StringBuilder();
		int len = 1;
		for (int i = 0; i < len; i++) {
			sb.append("0123456789");
		}
		String content = sb.toString();
		try {
			long l = System.currentTimeMillis();
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					for (int k = 0; k < 10; k++) {
						Message m = new Message();
						m.setTarget(target);
						m.addRecipient("queue1Consumer1");
						m.setContent(content);
						sb.setLength(0);
						sb.append("DEBUG: flow").append(i).append("/").append(j);
						sb.append("/step").append(k).toString();
						m.setKey(sb.toString());
						m.setId(RandomUtil.genUUID());
						try {
							Messenger.send(m);
						} catch (Exception e) {
							e.printStackTrace();
						}
						Thread.sleep(10L);
					}
				}
			}
			log.info("Queue messages in {}ms", System.currentTimeMillis() - l);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void testDubboMessageAdapter(ClassPathXmlApplicationContext ctx) {
		MessageService ms = (MessageService) ctx.getBean("serverMsgSvc");
		Message m = new Message();
		try {
			ms.put(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
